// const { printTemplate, connectBluetooth, getAllservice } = require('../../utils/blue.js');
//加载公共方法
const app = getApp()
import request from '../../utils/httpclient.js'
// pages/home/home.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showView: 'true',
  },
  testapi: function () {
    let params = {

      "GoodsNo": "",
      "keywords": "",
      "StoreInfoId": 1,
      "PageSize": 25,
      "CurrentPage": 0
    }
    let d = request('/Goods/QueryGoodsInfo',params)
    d.then((res)=> {
      console.log(res)
    }).catch((e)=> {
      console.log(e)
    })
    
  },
  goTooth: function (){
    wx.navigateTo({
      url: '../search/search'
    })
  },
  bluePt: function () {
    let data = wx.getStorageSync('meg')
    wx.getConnectedBluetoothDevices({
      success: function (res) {
        console.log(res)
        console.log("1223")
        if (res.devices.length == 0) {
          wx.showToast({
            title: '打印机连接中',
          });
          connectBluetooth(data.deviceId, function () {
            console.log("回调成功1");
            console.log(data);
            wx.showToast({
              title: '打印中...',
            })
            printTemplate(data.deviceId, data.serviceId, data.characteristicId)
            wx.showToast({
              title: '打印完成',
            })
            setTimeout(function () {
              wx.hideToast()
            }, 1500)
            console.log("zsm1", data)
          })
        } else {
          wx.showToast({
            title: '打印中...',
          })
          printTemplate(data.deviceId, data.serviceId, data.characteristicId)
          wx.showToast({
            title: '打印完成',
          })
          setTimeout(function () {
            wx.hideToast()
          }, 1500)
          console.log("zsm2", data)
        }

      }
    })

  },

  //步骤二：在扫描和初始化蓝牙
  // 开始扫描 411
  onScanClick: function (event) {
    console.log('扫描开始')
    let self = this
    wx.openBluetoothAdapter({
      success: function (res) {
        // 扫描蓝牙
        self.bleDisCovery()
        self.setData({
          bleAdapterStatus: "初始化成功"
        })
      },
      fail: function (error) {
        self.setData({
          bleAdapterStatus: "初始化失败"
        })
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '设备蓝牙未打开，请打开蓝牙功能',
          success: function (res) {
            if (res.confirm) {
              //console.log('用户点击确定')
            }
          }
        });
      },
      complete: function () {
        //console.log('complete')
      }
    });
  },
  //扫描设备 411
  bleDisCovery: function () {
    console.log("扫描蓝牙")
    let self = this
    wx.startBluetoothDevicesDiscovery({
      interval: 1000,
      success: function (res) {
        self.bleFound();
      }
    });
  },
  //发现设备信息 411
  bleFound: function () {
    console.log("发现设备信息")
    let self = this
    wx.onBluetoothDeviceFound(function (res) {
      let devices = res.devices
      console.log('发现设备', devices)
      let length = self.data.bleChips.length
      let devicesLength = devices.length
      if (devicesLength > length) {
        self.data.bleChips = devices
        self.setData({
          bleChips: devices
        });
      }
      console.log('显示设备', self.data.bleChips)
    });

  },
  
  //初始化蓝牙 411 
  bleInit: function () {
    console.log('初始化蓝牙')
    let self = this
    wx.openBluetoothAdapter({
      success: function (res) {
        self.setData({
          bleAdapterStatus: "初始化成功"
        })
      },
      fail: function (msg) {
        self.setData({
          bleAdapterStatus: "初始化失败"
        })
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '设备蓝牙未打开，请打开蓝牙功能',
          success: function (res) {
            if (res.confirm) {
              //console.log('用户点击确定')
              // 退出小程序
            }
          }
        });
      }
    });
  },
  // 蓝牙设备监听 411
  bleStatusListener: function () {
    // 监听蓝牙状态
    let slef = this
    wx.onBluetoothAdapterStateChange(function (res) {
      console.log(`adapterState changed, now is`, res)
      if (res.available) {
        // 是否可用
        console.log("蓝牙状态以改变！")
        slef.setData({
          bleStatus: "蓝牙已打开"
        });
      } else {
        slef.setData({
          bleStatus: "蓝牙已关闭"
        });
        // 不可用时
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '设备蓝牙未打开，请打开蓝牙功能',
          success: function (res) {
            if (res.confirm) {
              console.log('用户点击确定')
              // 退出小程序
            }
          }
        });
      }
    });
  },
  //步骤二：结束

  //连接蓝牙打印机
  onConnBle: function (e) {

    // 停止扫描
    wx.stopBluetoothDevicesDiscovery({
      success: function (res) {
      },
    });
    // 接收点击事件的参数
    let device = e.currentTarget.dataset.item
    console.log(`conn ble >> ${device}`)
    this.setData({
      bleChipInfo: device,
      // 显示隐藏
      showView: (!this.data.showView)
    })
    let deviceId = device.deviceId
    wx.setStorageSync('deviceId', deviceId)
    console.log("zsm", deviceId)
    let self = this
    // 连接设备
    console.log("连接设备中...")
    wx.createBLEConnection({
      deviceId: deviceId,
      success: function (res) {
        console.log("连接设备", deviceId),
          wx.showToast({
            title: '连接成功',
          });
          wx.getBLEDeviceServices(deviceId, function (data) { //获取蓝牙服务
          console.log("获取蓝牙服务", data)
          console.log(data.deviceId, data.serviceId, data.characteristicId)
          let meg = {
            deviceId: data.deviceId,
            serviceId: data.serviceId,
            characteristicId: data.characteristicId
          }
          console.log("meg", meg)
          wx.setStorageSync('meg', meg)
        })
        // 连接成功，打开 notify
        setTimeout(function () {
          self.bleServices(deviceId)
        }, 1500)

      },
      fail: function (errMsg) {
        console.log("errMsg", errMsg)
        wx.showToast({
          title: `连接失败:${errMsg}`,
        })
      }
    });
  },
  bleServices: function (deviceId) {
    let self = this
    wx.getBLEDeviceServices({
      deviceId: deviceId,
      success: function (res) {
        wx.showToast({
          title: 'service success',
        })
        let services = res.services
        for (let index in services) {
          let service = services[index]
          console.log(service)
          if (service.uuid === '6E400001-B5A3-F393-E0A9-E50E24DCCA9E' || service.uuid === '0000FEE7-0000-1000-8000-00805F9B34FB') {
            console.log("have service: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
            self.bleServiceChart(deviceId, service.uuid)
          }
        }
        console.log('device services:', res.services)
      }
    })
  },
  bleServiceChart: function (deviceId, serviceId) {
    let self = this;
    wx.getBLEDeviceCharacteristics({
      // 这里的 deviceId 需要在上面的 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: deviceId,
      // 这里的 serviceId 需要在上面的 getBLEDeviceServices 接口中获取
      serviceId: serviceId,
      success: function (res) {
        console.log('device getBLEDeviceCharacteristics:', res.characteristics)
        let characteristics = res.characteristics
        for (let index in characteristics) {
          let characteristic = characteristics[index]
          if (characteristic.uuid === '6E400001-B5A3-F393-E0A9-E50E24DCCA9E') {
            console.log("have characteristic: 6E400001-B5A3-F393-E0A9-E50E24DCCA9E")
          }
          console.log(characteristic)
        }
        self.openNotify(deviceId)
      }
    })
  },
  openNotify: function (deviceId) {
    this.setData({
      bleConnSuccess: true
    });
    let self = this
    wx.notifyBLECharacteristicValueChange({
      deviceId: deviceId,
      serviceId: '49535343-FE7D-4AE5-8FA9-9FAFD205E455',
      characteristicId: '0000FEE7-0000-1000-8000-00805F9B34FB',
      state: true,
      success: function (res) {
        console.log('notify success')
        self.onNotifyChange()
        wx.showToast({
          title: 'notify success',
        });
      },
      fail: function (err) {
        console.log(err)
        wx.showToast({
          title: 'notify fail',
        });
      }
    });
  },
  onNotifyChange: function () {
    // 接收数据
    let self = this
    wx.onBLECharacteristicValueChange(function (res) {
      console.log(res.characteristicId)
      let byteDatas = Array.from(new Int8Array(res.value))
      console.log(byteDatas)
      const data = byteDatas.join(',')
      self.setData({
        bleNotifyData: data
      });
      console.log(data)
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.openBluetoothAdapter) {
      wx.openBluetoothAdapter()
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    // this.testapi()
    showView: (options.showView == "true" ? true : false)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // 监听蓝牙
    // this.bleStatusListener()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 初始化蓝牙
    // this.bleInit()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.closeBluetoothAdapter({
      success: function (res) {
        console.log(res)
      },
      fail: function (e){
        console.log(e)
      }
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})